//! 姓名生成器测试
//! 
//! 测试 src/generators/name.rs 中的姓名生成功能

use dataforge::generators::name::*;

#[test]
fn test_zh_cn_fullname_generation() {
    for _ in 0..10 {
        let name = zh_cn_fullname();
        assert!(!name.is_empty());
        // 使用字符数统计（原len()统计字节数会导致误差）
        let char_count = name.chars().count();
        assert!((2..=4).contains(&char_count), "名字长度异常: {} ({}字符)", name, char_count);
        println!("中文全名: {}", name);
    }
}

#[test]
fn test_zh_cn_firstname_generation() {
    for _ in 0..10 {
        let firstname = zh_cn_firstname();
        assert!(!firstname.is_empty());
        // 允许1-2个字符
        let char_count = firstname.chars().count();
        assert!([1, 2].contains(&char_count), "异常名字: {} ({}字符)", firstname, char_count);
        println!("中文名: {}", firstname);
    }
}

#[test]
fn test_zh_cn_lastname_generation() {
    for _ in 0..10 {
        let lastname = zh_cn_lastname();
        assert!(!lastname.is_empty());
        // 允许1-2个字符（包含复姓）
        let char_count = lastname.chars().count();
        assert!((1..=2).contains(&char_count), "异常姓氏: {} ({}字符)", lastname, char_count);
        println!("中文姓: {}", lastname);
    }
}

#[test]
fn test_en_us_fullname_generation() {
    for _ in 0..10 {
        let name = en_us_fullname();
        assert!(!name.is_empty());
        assert!(name.contains(' ')); // 英文全名应该包含空格
        println!("英文全名: {}", name);
    }
}

#[test]
fn test_en_us_firstname_generation() {
    for _ in 0..10 {
        let firstname = en_us_firstname();
        assert!(!firstname.is_empty());
        assert!(!firstname.contains(' ')); // 名字不应该包含空格
        println!("英文名: {}", firstname);
    }
}

#[test]
fn test_en_us_lastname_generation() {
    for _ in 0..10 {
        let lastname = en_us_lastname();
        assert!(!lastname.is_empty());
        assert!(!lastname.contains(' ')); // 姓氏不应该包含空格
        println!("英文姓: {}", lastname);
    }
}

#[test]
fn test_ja_jp_fullname_generation() {
    for _ in 0..10 {
        let name = ja_jp_fullname();
        assert!(!name.is_empty());
        println!("日文全名: {}", name);
    }
}

#[test]
fn test_name_uniqueness() {
    let mut names = std::collections::HashSet::new();
    
    // 生成100个中文姓名，检查重复率
    for _ in 0..100 {
        let name = zh_cn_fullname();
        names.insert(name);
    }
    
    // 重复率应该很低（至少80%是唯一的）
    assert!(names.len() >= 80);
}

#[test]
fn test_name_format_consistency() {
    for _ in 0..20 {
        let zh_name = zh_cn_fullname();
        let en_name = en_us_fullname();
        
        // 中文姓名不应该包含空格
        assert!(!zh_name.contains(' '));
        
        // 英文姓名应该包含空格
        assert!(en_name.contains(' '));
        
        // 姓名不应该包含数字或特殊字符
        assert!(!zh_name.chars().any(|c| c.is_ascii_digit()));
        assert!(!en_name.chars().any(|c| c.is_ascii_digit()));
    }
}

#[test]
fn test_name_length_distribution() {
    let mut length_counts = std::collections::HashMap::new();
    
    for _ in 0..100 {
        let name = zh_cn_fullname();
        let len = name.chars().count();
        *length_counts.entry(len).or_insert(0) += 1;
    }
    
    // 中文姓名长度应该主要分布在2-4个字符
    assert!(length_counts.keys().all(|&len| len >= 2 && len <= 4));
    
    // 3个字符的姓名应该是最常见的
    let max_count = length_counts.values().max().unwrap();
    let most_common_length = length_counts
        .iter()
        .find(|(_, &count)| count == *max_count)
        .map(|(&len, _)| len)
        .unwrap();

    assert!(most_common_length >= 2 && most_common_length <= 4, "姓名长度分布不符合预期");
}